package com.lul15834.orderfood.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.ManagerAddFoodActivity;
import com.lul15834.orderfood.activity.user.listen.StartListen;
import com.lul15834.orderfood.dao.CommentDao;
import com.lul15834.orderfood.until.FileImgUntil;
import com.lul15834.orderfood.until.Tools;

import java.util.Objects;

public class UserComment extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;
    private ActivityResultLauncher<Intent> cameraLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_comment);
        Toolbar toolbar=findViewById(R.id.user_comment_back);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        EditText text=findViewById(R.id.user_comment_text);
        ImageView one=findViewById(R.id.user_comment_one);
        ImageView two=findViewById(R.id.user_comment_two);
        ImageView three=findViewById(R.id.user_comment_three);
        ImageView four=findViewById(R.id.user_comment_four);
        ImageView five=findViewById(R.id.user_comment_five);
        TextView con=findViewById(R.id.user_comment_con);
        one.setOnClickListener(new StartListen(UserComment.this));
        two.setOnClickListener(new StartListen(UserComment.this));
        three.setOnClickListener(new StartListen(UserComment.this));
        four.setOnClickListener(new StartListen(UserComment.this));
        five.setOnClickListener(new StartListen(UserComment.this));
        //实现拍照功能，以及获取相册功能
        ImageView img=findViewById(R.id.user_comment_img);
        Button camera=findViewById(R.id.user_comment_camera);
        Button photo=findViewById(R.id.user_comment_photo);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera1();
            }
        });
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery(v);
            }
        });
        String account=Tools.getOnAccount(this);
        Intent intent=getIntent();
        String businessId = Objects.requireNonNullElse(
                intent.getStringExtra("businessId"),
                "root"
        );
        Button btn=findViewById(R.id.user_comment_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text1=text.getText().toString();
                if (text1.isEmpty()){
                    text.setError("请输入评论");
                }else {

                    String path= FileImgUntil.getImgName();//图片存储路径名字
                    Drawable drawable=img.getDrawable();
                    if (drawable==null){
                        path="";
                    }else {
                        Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
                        FileImgUntil.saveImgToPath(bitmap, path);
                    }
                    String score=con.getText().toString();
                    String conZ[]={"非常差","差","一般","满意","非常满意"};
                    int sc=1;
                    for (int i=0;i<conZ.length;i++){
                        if (conZ[i].equals(score)){
                            sc=i+1;
                            break;
                        }
                    }
                    int a=CommentDao.insertComment(account,businessId,text1,String.valueOf(sc),path);
                    if (a==1){
                        Toast.makeText(UserComment.this, "评论成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UserComment.this, "评论失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        getContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(),
                result -> {
                    if (result != null) {
                        img.setImageURI(result);
                    }
                });
        cameraLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result->{
            if (result.getResultCode()==Activity.RESULT_OK){
                Intent data=result.getData();
                if (data!=null){
                    Bundle bundle=data.getExtras();
                    if (bundle!=null&&bundle.containsKey("data")){
                        Bitmap bitmap=(Bitmap) bundle.get("data");
                        img.setImageBitmap(bitmap);
                    }else {
                        //选择相册
                        Uri selectImg=data.getData();
                        img.setImageURI(selectImg);
                    }
                }
            }
        });
    }
    private void camera1(){
        Intent pic=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraLauncher.launch(pic);
    }
    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }
}